#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int cost = tickets[k];
        int res = 0, n = tickets.size();
        for (int i = 0; i < n; i++)
        {
            if (i <= k)
                res += min(cost, tickets[i]);
            else
                res += min(cost - 1, tickets[i]);
        }
        return res;
    }
};